﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Net.Fan.SmallPig.DataBase;
using System.Data.SqlClient;

namespace Net.Fan.SmallPig.DataAccess
{
    public class RiskDA
    {
        public static DataTable GetRisks()
        {
            string sql = "select r.*,j.* from Risks r ,Jobs j where r.JobId=j.JobId order by j.PlanFinishTime desc";

            //SqlParameter[] paras = new SqlParameter[0];
            //paras[0] = new SqlParameter("@UserId", userId);

            DataSet ds = DA_Base.RunReturnDataset(sql);
            return ds.Tables[0];
        }


        public static long AddNewRisk(long jobId, string type)
        {

            string sql = "INSERT INTO Risks (JobId, Type,CheckTime) VALUES (@JobId, @Type,GetDate()) ";
            sql += " select @@identity";
            SqlParameter[] paras = new SqlParameter[2];
            paras[0] = new SqlParameter("@JobId", jobId);
            paras[1] = new SqlParameter("@Type", type);

            decimal dd = (decimal)DA_Base.ExecuteScalar(sql, paras);
            return Convert.ToInt64(dd);
        }

        public static void CheckOverDue()
        {
            //检查过期的任务
            string sql = "exec CheckOverdue ";
            DA_Base.ExecuteNonQuery(sql);
        }

        public static void CheckDueSoon(int differenceDays)
        {
            string sql = "exec CheckDueSoon @Difference = " + differenceDays;
            DA_Base.ExecuteNonQuery(sql);
        }
    }
}
